package com.caffedev.hangulnori;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class HangulnoriMain extends Activity {

	final static String TAG = "MainActivity";
	private static final String DATABASE_NAME = "base.db";
	private static final String DATABASE_DIR = "/data/data/com.caffedev.hangulnori/databases/";
	private static final String DATABASE_ASSETS = "database/base.db";
	 
	public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_NO_TITLE);
       
       setContentView(R.layout.main);
     //MODE 
       /*
        * animal
        * vehicle
        * thing
        * food
        * number
        * family
        * 
        * */
    
       ImageView animal = (ImageView)findViewById(R.id.id_play_animal);
       ImageView vehicle = (ImageView)findViewById(R.id.id_play_vehicle);
       ImageView thing = (ImageView)findViewById(R.id.id_play_thing);
       ImageView food = (ImageView)findViewById(R.id.id_play_food);
       ImageView number = (ImageView)findViewById(R.id.id_play_number);
       ImageView family = (ImageView)findViewById(R.id.id_play_family);

       
       animal.setOnClickListener(listener);
       vehicle.setOnClickListener(listener);
       thing.setOnClickListener(listener);
       food.setOnClickListener(listener);
       number.setOnClickListener(listener);
       family.setOnClickListener(listener);

       copyDatabase();
   }
   

   public void copyDatabase(){

   	File folder = new File(DATABASE_DIR);
   	folder.mkdirs();
   	
   	
       File file = new File(DATABASE_DIR + DATABASE_NAME);
       
       if(file.exists()){
       	Log.e(TAG,"database exists");
       	return;
       }
       
       		AssetManager assetManager = getResources().getAssets();
       		
       		try{
	        		InputStream is = assetManager.open(DATABASE_ASSETS, AssetManager.ACCESS_BUFFER);
	        		OutputStream out = new FileOutputStream(file);
	        		writeFile(is, out);
	        		out.close();
	        		is.close();
       		}catch (Exception e){
       			e.printStackTrace();
       		}

   }
   public void writeFile(InputStream is, OutputStream os) throws IOException
   {
        int c = 0;
        while((c = is.read()) != -1)
            os.write(c);
        os.flush();
   }  
   //MODE 
   /*
    * animal
    * vehicle
    * thing
    * food
    * number
    * family
    * 
    * */
   
   
	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			
			Intent intent = new Intent(HangulnoriMain.this, HangulnoriPlay.class);
			switch (v.getId()){
			case R.id.id_play_animal:
	    		intent.putExtra("MODE", "animal");
				startActivity(intent);
				break;
			case R.id.id_play_vehicle:
	    		intent.putExtra("MODE", "vehicle");
				startActivity(intent);
				break;	
			case R.id.id_play_thing:
	    		intent.putExtra("MODE", "thing");
				startActivity(intent);
				break;
			case R.id.id_play_food:
	    		intent.putExtra("MODE", "food");
				startActivity(intent);
				break;
			case R.id.id_play_number:
	    		intent.putExtra("MODE", "number");
				startActivity(intent);
				break;
			case R.id.id_play_family:
	    		intent.putExtra("MODE", "family");
				startActivity(intent);
				break;

				

			}
		}
	};


	@Override
	protected void onDestroy() {
		Log.e(TAG,"onDestory");
		
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.e(TAG,"onPause");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.e(TAG,"onRestart");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.e(TAG,"onResume");
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.e(TAG,"onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.e(TAG,"onStop");
		super.onStop();
	}



}
